package com.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.comm.DataAndView;
import com.erp.comm.ResultCode;
import com.erp.pojo.Customer;
import com.erp.pojo.Provider;
import com.erp.service.ProviderService;
import com.erp.vo.CustomerVo;
import com.erp.vo.ProviderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("provider")
public class ProviderController {
    @Autowired
    private ProviderService providerService;


    @RequestMapping("loadAllProvider")
    public DataAndView loadAllCustomer(ProviderVo providerVo) {


        IPage<Provider> page = new Page<>(providerVo.getPage(), providerVo.getLimit());
        //条件查询
        QueryWrapper<Provider> qw = new QueryWrapper<>();
        if (providerVo.getProvidername() != null) {
            qw.like("providername", providerVo.getProvidername());
        }
        if (providerVo.getPhone() != null) {
            qw.like("telephone", providerVo.getPhone());
        }
        if (providerVo.getConnectionperson() != null) {
            qw.like("connectionperson", providerVo.getConnectionperson());
        }
           providerService.page(page,qw);
        DataAndView da = new DataAndView(page.getTotal(), page.getRecords());
        return da;
    }
    //
    //添加

    @RequestMapping("addProvider")
    public ResultCode addProvider(Provider provider) {
        //获取用户填写的数据
//        System.out.println(customer);
        provider.setId(null);
        boolean bool = providerService.save(provider);
        try {
            if (bool) {
                return ResultCode.SUCCESS_ADD;
            } else {
                return ResultCode.FAIL_ADD;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_ADD;
        }

    }

    //修改
    @RequestMapping("updateProvider")
    public ResultCode updateProvider(Provider provider) {

        try {
            boolean bool = providerService.updateById(provider);
            if (bool) {
                return ResultCode.SUCCESS_UPDATE;
            } else {
                return ResultCode.FAIL_UPDATE;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_UPDATE;
        }

    }


    //删除
    @RequestMapping("deleteProvider")
    public ResultCode deleteProvider(Provider provider) {

        try {
            boolean bool = providerService.removeById(provider);
            if (bool) {
                return ResultCode.SUCCESS_DELETE;
            } else {
                return ResultCode.FAIL_DELETE;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_DELETE;
        }

    }

    //批量删除
    @RequestMapping("batchDeleteProvider")
    public ResultCode batchDeleteProvider(ProviderVo providerVo) {
        List<Integer> list =new ArrayList<>();
        Integer[] ids =providerVo.getIds();
        for (Integer id:ids){
            list.add(id);
        }
        try {
            boolean info= providerService.removeByIds(list);
            if (info) {
                return ResultCode.SUCCESS_DELETE;
            } else {
                return ResultCode.FAIL_DELETE;
            }
        }catch (Exception e){
            return ResultCode.FAIL_DELETE;
        }

    }
    @RequestMapping("loadAllProviderForSelect")
    public  DataAndView loadAllProviderSelect(){
         List<Provider> list=providerService.list();

        return new DataAndView(list);
    }

}
